//
//  LHRecommendTourNotesView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-19.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendTourNotesView.h"
#import "LHRecommendTourNotesTableViewCell.h"
#import "LHRecommendTravelNotesModel.h"
#import "LHNetworkHandle.h"
#import "UIScrollView+MJRefresh.h"
#import "UIImageView+WebCache.h"

@interface LHRecommendTourNotesView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tourNotesTableView;
@property (nonatomic, retain) NSMutableArray *tourNotesArr;
@property (nonatomic, retain) NSMutableArray *freshTempArr;
@property (nonatomic, retain) UIView *firstView;
@property (nonatomic, retain) UIImageView *notePic;
@property (nonatomic, retain) UILabel *countryName;
@property (nonatomic, retain) UILabel *themeName;
@property (nonatomic, retain) UIImageView *userPic;
@property (nonatomic, retain) UILabel *userName;
@property (nonatomic, retain) UIView *secondView;
@property (nonatomic, retain) UIImageView *jianTou;
@property (nonatomic, retain) UIButton *moreNotes;

@end

@implementation LHRecommendTourNotesView

- (void)dealloc
{
    [_tourNotesTableView release];
    [_tourNotesArr release];
    [_freshTempArr release];
    [_firstView release];
    [_notePic release];
    [_countryName release];
    [_themeName release];
    [_userPic release];
    [_secondView release];
    [_jianTou release];
    [_userName release];
    [super dealloc];
}

/**
 *  初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.freshTempArr = [NSMutableArray array];
        
        [self getData];
    }
    
    
    return self;
}

/**
 *  创建游记tableView
 */
- (void)createTourNotesTableView
{
    self.firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LHWidth, LHheight - 124)];
    
    _firstView.backgroundColor = [UIColor brownColor];
    
    [self addSubview:_firstView];
    
    [_firstView release];
    
    self.notePic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LHWidth, LHheight - 60 - 250)];
    
    _notePic.backgroundColor = [UIColor colorWithRed:0.137 green:0.370 blue:0.067 alpha:1.000];
    
    [_firstView addSubview:_notePic];
    
    [_notePic release];
    
    self.countryName = [[UILabel alloc] initWithFrame:CGRectMake(20, LHheight - 290, 150, 40)];
    
    _countryName.backgroundColor = [UIColor colorWithRed:0.126 green:0.889 blue:0.830 alpha:1.000];
    
    [_firstView addSubview:_countryName];
    
    [_countryName release];
    
    self.themeName = [[UILabel alloc] initWithFrame:CGRectMake(20, _countryName.frame.origin.y + _countryName.frame.size.height + 20, 300, 30)];
    
    _themeName.backgroundColor = [UIColor colorWithRed:0.929 green:0.165 blue:0.078 alpha:1.000];
    
    [_firstView addSubview:_themeName];
    
    [_themeName release];
    
    self.userPic = [[UIImageView alloc] initWithFrame:CGRectMake(20, _themeName.frame.origin.y + _themeName.frame.size.height + 20, 40, 40)];
    
    _userPic.backgroundColor = [UIColor whiteColor];
    
    [_firstView addSubview:_userPic];
    
    [_userPic release];
    
    self.userName = [[UILabel alloc] initWithFrame:CGRectMake(_userPic.frame.origin.x + _userPic.frame.size.width + 10, _themeName.frame.origin.y + _themeName.frame.size.height + 20, 80, 20)];
    
    _userName.backgroundColor = [UIColor colorWithRed:0.100 green:0.733 blue:0.945 alpha:1.000];
    
    [_firstView addSubview:_userName];
    
    [_userName release];
    
    self.secondView = [[UIView alloc] initWithFrame:CGRectMake(0, LHheight - 124, LHWidth, LHheight)];
    
    _secondView.backgroundColor = [UIColor colorWithRed:0.088 green:0.416 blue:0.691 alpha:1.000];
    
    [self addSubview:_secondView];
    
    [_secondView release];
    
    self.jianTou = [[UIImageView alloc] initWithFrame:CGRectMake(LHWidth / 2 - 15, 10, 30, 20)];
    
    _jianTou.backgroundColor = [UIColor colorWithRed:0.415 green:0.541 blue:0.075 alpha:1.000];
    
    [_secondView addSubview:_jianTou];
    
    [_jianTou release];
    
    self.moreNotes = [UIButton buttonWithType:UIButtonTypeSystem];
    
    _moreNotes.frame = CGRectMake(LHWidth / 2 - 30, _jianTou.frame.origin.y + _jianTou.frame.size.height + 10, 60, 20);
    
    _moreNotes.backgroundColor = [UIColor colorWithRed:0.655 green:0.000 blue:0.598 alpha:1.000];
    
    [_secondView addSubview:_moreNotes];
    
    
    self.tourNotesTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    [_tourNotesTableView addFooterWithTarget:self action:@selector(freshData)];
    [_tourNotesTableView setFooterRefreshingText:@"客官,等一等"];
    
    [_tourNotesTableView addHeaderWithTarget:self action:@selector(tempData)];
    [_tourNotesTableView setHeaderRefreshingText:@"我正在用力~~"];
    
    _tourNotesTableView.delegate = self;
    _tourNotesTableView.dataSource = self;
    
    [_secondView addSubview:_tourNotesTableView];
    
    [_tourNotesTableView release];
    
}

#pragma mark - tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _freshTempArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommendTourNotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kk"];
    
    if (cell == nil) {
        
        cell = [[[LHRecommendTourNotesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kk"]autorelease];
    }
    
    cell.tourNotes = _freshTempArr[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.delegate jumpToNotesDetailViewWithIndexPath:indexPath tourNotesArr:_freshTempArr];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.height * 2 / 5 + 20;
}

/**
 *  获取数据
 */

- (void)getData
{
    [LHNetworkHandle getDataWithURL:@"http://api.breadtrip.com/trips/hot/?start=0&count=50&is_ipad=true" completion:^(id result) {
       
        self.tourNotesArr = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"trips"];
        

        
        for (NSDictionary *tempDic in arr) {
            
            LHRecommendTravelNotesModel *recTourNote = [[LHRecommendTravelNotesModel alloc] initWithDic:tempDic];
            
            [_tourNotesArr addObject:recTourNote];

            [recTourNote release];
        }
        
        for (NSInteger i = 0; i < 10; i++) {
            
            [self.freshTempArr addObject:self.tourNotesArr[i]];
        }
        
        
        
        [self createTourNotesTableView];
        
        [_notePic sd_setImageWithURL:[NSURL URLWithString:[[_tourNotesArr objectAtIndex:0] cover_image_1600]]];
        
        _countryName.text = [[_tourNotesArr objectAtIndex:0] popular_place_str];
        
        _themeName.text = [[_tourNotesArr objectAtIndex:0] name];
        
//        [_tourNotesUserPic sd_setImageWithURL:[NSURL URLWithString:[tourNotes.user objectForKey:@"avatar_l"]]];
        
//        [_userPic sd_setImageWithURL:[NSURL URLWithString:[[_tourNotesArr objectAtIndex:0] user]]];
        
//        NSLog(@"%@",_tourNotesArr);
//        _userLabel.text = [tourNotes.user objectForKey:@"name"];
        
//        _userName.text = [[[_tourNotesArr objectAtIndex:0] user]];
//        [[_tourNotesArr objectAtIndex:0]user]
//        NSMutableDictionary *temp = [[_tourNotesArr objectAtIndex:0]user];
        
    }];
}


- (void)freshData
{
    if (self.freshTempArr.count != self.tourNotesArr.count) {
        
        NSInteger j = self.freshTempArr.count;
        
        for (NSInteger i = j; i < j + 5; i ++) {
            
            [self.freshTempArr addObject:self.tourNotesArr[i]];
        }
    }
    
    [_tourNotesTableView reloadData];
    [_tourNotesTableView footerEndRefreshing];
}

- (void)tempData
{
    [_tourNotesTableView reloadData];
    [_tourNotesTableView headerEndRefreshing];
    
}



@end
